#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

void *threadRoutine(void *args)
{
    int i = 0;
    int *data = new int[10];
    while (true)
    {
        cout << "新线程: " << (char *)args << " running" << endl;
        sleep(1);
        data[i] = i;
        if (i++ == 5)
            break;
        //测试异常
        // int a = 10;
        // a /= 0;
    }
    cout << "new thread quit" << endl;
    return (void *)data;
    // return (void *)10;
}
int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void *)"thread1");
    // void *ret = nullptr;
    // pthread_join(tid, &ret);
    int *ret = nullptr;
    pthread_join(tid, (void **)&ret);
    //cout << "main thread wait done, main quit: new thread quit: " << (long long)ret << endl;
    cout << "main thread wait done, main quit: new thread quit" << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << ret[i] << " ";
    }
    cout << endl;
    return 0;
}